package com.rzico.sbl.ui.manager

import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.utils.observe
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityMasterBarrelShipBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.ui.order.OrderShipWhereActivity
import com.rzico.sbl.ui.order.OrderShipWhoActivity
import com.rzico.sbl.viewmodel.MasterViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common_ui.base.BaseActivity

class MasterBarrelShipActivity : BaseActivity(R.layout.activity_master_barrel_ship) {

    private val mBinding by lazy {
        ActivityMasterBarrelShipBinding.bind(getRootView())
    }

    private var mShopId = ""
    private var mAdminId = ""

    override fun getViewModel() = get<MasterViewModel>()

    override fun initData() {
        initTitle(title = "转派")
    }

    override fun initListener() {
        mBinding.apply {
            shipWhereLl.oneClick { startActivityEx<OrderShipWhereActivity>() }

            shipWhoLl.oneClick {
                if (mShopId.isEmpty()) {
                    showToast("请选择配送人员")
                    return@oneClick
                }

                startActivityEx<OrderShipWhoActivity>("shopId" to mShopId)
            }

            btShip.oneClick {
                when {
                    mShopId.isEmpty() -> {
                        showToast("请选择配送站")
                        return@oneClick
                    }

                    mAdminId.isEmpty() -> {
                        showToast("请选择配送人员")
                        return@oneClick
                    }
                }

                getViewModel()
                    .request(
                        requestUrl = BaseUrl.mngDispatch,
                        isMultipart = true,
                        params = arrayOf(
                            "id" to intent.getExtra("barrelId"),
                            "shopId" to mShopId,
                            "adminId" to mAdminId,
                            "memo" to shipMemo.content
                        ),
                        onSuccess = {
                            showToast("退桶转派成功！")

                            // 更新退桶列表
                            LiveEventBus
                                .get("barrelList")
                                .post("refresh")

                            finishView()
                        }
                    )
            }
        }

        observe<String>("orderShip") {
            val items = it.split(",")
            when (items[0]) {
                "shop" -> {
                    mShopId = items[1]
                    mAdminId = ""

                    mBinding.apply {
                        shipWhere.text = items[2]
                        shipWho.text = ""
                    }
                }

                "admin" -> {
                    mAdminId = items[1]
                    mBinding.shipWho.text = items[2]
                }
            }
        }
    }

}